package com.onenine.app.activity;

import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.util.Pair;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.onenine.app.R;
import com.onenine.app.adapter.ReplyMsgAdapter;
import com.onenine.app.model.PraiseMsgItem;
import com.onenine.app.request.CommonRequest;
import com.onenine.app.response.ReplyMsgResponse;
import com.onenine.app.utils.CDataProcessor;
import com.onenine.app.utils.DataType;
import com.onenine.app.utils.UrlConfig;
import com.onenine.base.base.BasePageListActivity;
import com.onenine.base.okhttp.OkCallback;
import com.onenine.base.okhttp.OkRequest;
import com.onenine.base.view.PullRefreshRecyclerView;
import com.onenine.chat.rongcloud.RcUser;
import com.onenine.dialog.CommonTextDialog;

/**
 * 消息-回复列表
 */
public class ReplyListActivity extends BasePageListActivity
        implements View.OnClickListener, BaseQuickAdapter.OnItemClickListener {

    @Override
    protected Pair<PullRefreshRecyclerView, RecyclerView.Adapter> initPullRefreshView() {
        return Pair.create(findViewById(R.id.pull_to_refresh_layout), new ReplyMsgAdapter());
    }

    @Override
    protected void initViews() {
        super.initViews();
        mPullRefreshView.setEnableLoadMore(false);
        mToolbar.setCenterTitle(R.string.reply);
        mToolbar.setRightTitle(R.string.clear_all);
    }

    @Override
    protected void initData() {
        super.initData();
        loadPageData(1);
    }

    @Override
    protected void initListeners() {
        super.initListeners();
        mToolbar.setOnRightClickListener(this);
        mPullRefreshView.setOnItemClickListener(this);
    }

    @Override
    protected void loadPageData(int pageNum) {
        OkRequest.getInstance().withUrl(UrlConfig.GET_COMMENT_MSG_LIST)
                .withRequest(new CommonRequest(RcUser.getInstance().getUserId()))
                .doRequest(PraiseListActivity.class.getSimpleName(), new OkCallback<ReplyMsgResponse>() {
                    @Override
                    public void onResponse(ReplyMsgResponse response, String srcData) {
                        dismissRefreshStatus();
                        if (response.isSuccess()) {
                            notifyDataSuccess(response.getDataList());
                            mPullRefreshView.loadMoreEnd(true);
                        } else {
                            notifyDataFailure();
                        }
                    }
                });
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        int postType;
        PraiseMsgItem item = (PraiseMsgItem) adapter.getItem(position);
        if (item.getParamType() == DataType.PRAISE_DYNAMIC
                || item.getParamType() == DataType.PRAISE_DYNAMIC_COMMENT) {
            //动态
            postType = DataType.POST_DYNAMIC;
        } else if (item.getParamType() == DataType.PRAISE_ASK
                || item.getParamType() == DataType.PRAISE_ASK_COMMENT) {
            //问答
            postType = DataType.POST_ASK;
        } else {
            //帖子
            postType = DataType.POST_TOPIC;
        }
        PostDetailActivity.launchActivity(this
                , item.getMsgParams(), postType, 0);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_pull_refresh_layout;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_right_title) {
            showClearDialog();
        }
    }

    /**
     * 清空前确认
     */
    private void showClearDialog() {
        new CommonTextDialog.Builder(this)
                .setText(R.string.delete_all_message)
                .setOnOkClickListener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        CDataProcessor.clearMsgContentByType(DataType.MSG_REPLY_CLEAR
                                , PraiseListActivity.class.getSimpleName(), new Runnable() {
                                    @Override
                                    public void run() {
                                        onRefresh();
                                    }
                                });
                    }
                }).builder().show();
    }

}
